<html>
<title>service worker post message</title>
<script>
let clients;

async function register(worker_url, scope) {
  try {
    const init = scope ? {scope} : {};
    await navigator.serviceWorker.register(worker_url, init);
    await navigator.serviceWorker.ready;
    return 'DONE';
  } catch (error) {
    return `${error}`;
  }
}

async function executeCommandOnServiceWorker(command) {
  const registration = await navigator.serviceWorker.ready;
  return new Promise(resolve => {
    const channel = new MessageChannel();
    channel.port1.onmessage = e => { resolve(e.data); };
    registration.active.postMessage({command}, [channel.port2]);
  })
}

</script>
</html>
